import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';
import rollupTypescript from '@rollup/plugin-typescript';

import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';

import clean from './plugins/rollup-plugin-clean';

import url from 'postcss-url';

const env = process.env.BUILD;

let plugins = [];
let file = 'dist/seditor-dev.js';

if (env == 'prod') {
    plugins = [terser()];
    file = 'dist/seditor.js';
}

export default {
    input: 'src/index.ts',
    output: [
        {
            file,
            format: 'umd',
            name: 'seditor',
            plugins,
            globals: {
                leaflet: 'L',
            },
        },
    ],
    plugins: [
        clean(),
        rollupTypescript({
            allowSyntheticDefaultImports: true,
        }),
        resolve(),
        commonjs(),
        json(),
        babel({
            exclude: 'node_modules/**',
            babelHelpers: 'bundled',
        }),
        postcss({
            extract: true,
            extensions: ['.css', '.scss'],
            minimize: true,
            plugins: [url({ url: 'inline' })],
        }),
    ],
    external: ['leaflet'],
    onwarn(message, warn) {
        if (message.code === 'CIRCULAR_DEPENDENCY') return;
        warn(message);
    },
};
